package com.clkj.nangang.mvp.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.clkj.nangang.R;
import com.clkj.nangang.app.CustomApplication;
import com.clkj.nangang.common.Constant;
import com.clkj.nangang.mvp.MainActivity;
import com.clkj.nangang.mvp.base.BaseActivity;
import com.clkj.nangang.mvp.model.LoginUser;
import com.clkj.nangang.mvp.register.RegisterActivity;
import com.clkj.nangang.util.CommonUtil;
import com.clkj.nangang.util.schedulers.SchedulerProvider;


public class LoginActivity extends BaseActivity implements LoginContract.View, View.OnClickListener {

    private EditText loginEditAccount;
    private EditText loginEditPwd;
    private Button btLogin;
    private Button btRegist;

    private LoginContract.Presenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initData();
        initView();
        assignView();
    }

    @Override
    public void initData() {

        presenter = new LoginPresenter(this, SchedulerProvider.getInstance(), CustomApplication.getInstance().getHttpService());
    }

    @Override
    public void assignView() {
        btLogin.setOnClickListener(this);
        btRegist.setOnClickListener(this);
    }

    @Override
    public void initView() {

        loginEditAccount = (EditText) findViewById(R.id.login_edit_account);
        loginEditPwd = (EditText) findViewById(R.id.login_edit_pwd);
        btLogin = (Button) findViewById(R.id.bt_login);
        btRegist = (Button) findViewById(R.id.bt_regist);
    }

    @Override
    public void onNetError(String tip) {
        Toast.makeText(this, tip, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void LoginSuccess(LoginUser loginUser) {
        Toast.makeText(this, "登录成功!", Toast.LENGTH_SHORT).show();

        CommonUtil.putShareValue(Constant.LOGIN_STATUS, true);
        CommonUtil.putShareValue(Constant.NANGAN_USERID, loginUser.getUserid());
        CommonUtil.putShareValue(Constant.NANGAN_NAME, loginUser.getName());
        CommonUtil.putShareValue(Constant.NANGAN_PHONE, loginUser.getMobilePhoneNumber());
        CommonUtil.putShareValue(Constant.NANGAN_PWD, loginUser.getPassword());
        //小区还是组
        CommonUtil.putShareValue(Constant.NANGAN_QUANTERS, loginUser.getQuanters());
        CommonUtil.putShareValue(Constant.NANGAN_FLOOR, loginUser.getFloor());
        CommonUtil.putShareValue(Constant.NANGAN_ROOM, loginUser.getRoom());
        //角色
        CommonUtil.putShareValue(Constant.NANGAN_ROLE, loginUser.getRoleN());
        //城市
        CommonUtil.putShareValue(Constant.NANGAN_CZXQ, loginUser.getCZXQ());

        startAnimActivity(MainActivity.class);
        finish();
    }

    @Override
    public void LoginFail(String tip) {
        Toast.makeText(this, tip, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void setPresenter(LoginContract.Presenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //登录
            case R.id.bt_login:
                String phone = loginEditAccount.getText().toString();
                String pass = loginEditPwd.getText().toString();
                if (TextUtils.isEmpty(phone)) {
                    Toast.makeText(LoginActivity.this, "请输入手机号码",
                            Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(pass)) {
                    Toast.makeText(LoginActivity.this, "请输入密码",
                            Toast.LENGTH_SHORT).show();
                    return;
                }

                presenter.validateLogin(phone, pass);

                break;
            //去注册
            case R.id.bt_regist:
                Intent toRegister=new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(toRegister);
                break;
        }
    }
}
